package com.androwit.engine.mapping.converter.str;

import com.androwit.engine.mapping.Converter;

public class LabeledEnumConverter implements Converter<LabeledEnum, String> {

	protected LabeledEnum[] labeledEnum;

	public LabeledEnumConverter(LabeledEnum[] labeledEnum) {
		this.labeledEnum = labeledEnum;
	}

	@Override
	public LabeledEnum fromConverted(String value) throws Exception {
		for (LabeledEnum current : labeledEnum) {
			String label = current.getLabel();
			if (label == value || label.equals(value))
				return current;
		}
		throw new IllegalArgumentException(value);
	}

	@Override
	public String convert(LabeledEnum value) throws Exception {
		return value.getLabel();
	}

}
